package com.yanqu.road.server.pb;

import com.yanqu.road.entity.cricket.*;
import com.yanqu.road.entity.cricket.config.CricketCourtConfig;
import com.yanqu.road.entity.cricket.court.CricketCourtBattleData;
import com.yanqu.road.entity.cricket.court.CricketCourtDefenseData;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 蛐蛐系统
 */
public class CricketPb {

    /**
     * 用户数据
     */
    public static CricketProto.UserCricketDataTempMsg.Builder parseUserDataTemp(UserCricketData userCricketData, Map<Integer, CricketData> cricketDataMap) {
        if (userCricketData == null) {
            return null;
        }
        CricketProto.UserCricketDataTempMsg.Builder builder = CricketProto.UserCricketDataTempMsg.newBuilder();
        builder.setCatchEventTimes(userCricketData.getCatchEventTimes());
        builder.addAllCricketIdList(userCricketData.getCricketIdList());
        for (CricketData cricketData : cricketDataMap.values()) {
            builder.addCricket(parseCricketTemp(cricketData));
        }
        builder.setBadgeLevel(userCricketData.getBadgeLevel());
        builder.setCricketScore(userCricketData.getCricketScore());
        builder.setScenceId(userCricketData.getScenceId());
        for (CricketScenceData scenceData : userCricketData.getScenceDataMap().values()) {
            builder.addScence(parseCricketScenceTemp(scenceData));
        }

        builder.setTotalCatchTimes(userCricketData.getTotalCatchTimes());
        builder.setCanWorship(userCricketData.getWorshipTime() != userCricketData.getCourtRound());
        builder.setMatchTimes(userCricketData.getRemainMatchTimes());
        builder.addAllDefenseCricketId(userCricketData.getCricketMatchData().getDefenseList());
        builder.setDrawPieceCount(userCricketData.getDrawPieceCount());
        builder.setDrawLevel(userCricketData.getDrawLevel());
        builder.setDrawRewardLevel(userCricketData.getDrawRewardLevel());
        builder.setDrawReward(PropertyHelper.parsePropertyToString(userCricketData.getDrawReward()));
        builder.setGuideBit(userCricketData.getGuideBit());
        builder.setCourtBadgeId(userCricketData.getCourtBadgeId());
        builder.setCourtBadgeExp(userCricketData.getCourtBadgeExp());
        builder.setBuySliverToken(userCricketData.getBuySliverToken() > 0);
        builder.setBuyGoldToken(userCricketData.getBuyGoldToken() > 0);
        builder.setShowCourtRank(userCricketData.isShowCourtRank());
        builder.setSkillRecycleTime(userCricketData.getSkillRecycleTime());
        return builder;
    }


    /**
     * 蛐蛐信息模板
     *
     * @param cricketData 蛐蛐数据
     */
    public static CricketProto.CricketTempMsg.Builder parseCricketTemp(CricketData cricketData) {
        if (cricketData == null) {
            return null;
        }
        CricketProto.CricketTempMsg.Builder builder = CricketProto.CricketTempMsg.newBuilder();
        builder.setUserId(cricketData.getUserId());
        builder.setCricketId(cricketData.getCricketId());
        builder.setQuality(cricketData.getQuality());
        builder.setTitle(cricketData.getTitle());
        builder.setLittleTitle(cricketData.getLittleTitle());
        builder.setTalent(parseCricketTalentTemp(cricketData.getCricketTalent()));
        builder.setProperty(parseCricketPropertyTemp(cricketData.getCricketProperty()));
        builder.setAddProperty(parseCricketPropertyTemp(cricketData.getAddProperty()));
        for (CricketSkill cricketSkill : cricketData.getAttackSkillList()) {
            CricketProto.CricketSkillTempMsg.Builder temp = parseCricketSkillTemp(cricketSkill);
            if (temp != null) {
                builder.addAttackSkill(temp);
            }
        }
        for (CricketSkill cricketSkill : cricketData.getInitSkillList()) {
            CricketProto.CricketSkillTempMsg.Builder temp = parseCricketSkillTemp(cricketSkill);
            if (temp != null) {
                builder.addInitSkill(temp);
            }
        }
        for (CricketSkill cricketSkill : cricketData.getSkillList()) {
            CricketProto.CricketSkillTempMsg.Builder temp = parseCricketSkillTemp(cricketSkill);
            if (temp != null) {
                builder.addSkill(temp);
            }
        }
        for (CricketEquipSkill cricketSkill : cricketData.getEquipSkillList()) {
            CricketProto.CricketNormalSkillTempMsg.Builder temp = parseCricketNormalSkillTemp(cricketSkill);
            if (temp != null) {
                builder.addNormalSkill(temp);
            }
        }
        builder.setBattleTimes(cricketData.getBattleTimes());
        builder.setMatchTimes(cricketData.getMatchTimes());
        builder.setEquipPatronId(cricketData.getEquipPatronsId());
        builder.setEquipTimes(cricketData.getEquipTimes());
        builder.setEquipBuyTimes(cricketData.getEquipBuyTimes());
        builder.setRewardNum(cricketData.getRewardNum());

        return builder;
    }

    /**
     * 数据变动
     */
    public static CricketProto.CricketDataChangeTempMsg.Builder parseCricketDataChange(CricketData cricketData) {
        if (cricketData == null || cricketData.getChange() == null) {
            return null;
        }
        CricketProto.CricketDataChangeTempMsg.Builder builder = CricketProto.CricketDataChangeTempMsg.newBuilder();
        builder.setCricket(parseCricketTemp(cricketData));
        CricketDataChange change = cricketData.getChange();
        builder.setFirstGet(change.isFirstGet());
        builder.setIsGet(change.isGet());
        builder.setIsMax(change.isMax());
        if (!StringUtils.isNullOrEmpty(change.getTurnReward())) {
            builder.setTurnReward(change.getTurnReward());
        }
        builder.setCount(change.getCount());
        return builder;
    }

    /**
     * 蛐蛐资质(天生属性)
     */
    public static CricketProto.CricketTalentTempMsg.Builder parseCricketTalentTemp(CricketTalent cricketTalent) {
        if (cricketTalent == null) {
            return null;
        }
        CricketProto.CricketTalentTempMsg.Builder builder = CricketProto.CricketTalentTempMsg.newBuilder();
        builder.setAtk(cricketTalent.getAtk());
        builder.setHp(cricketTalent.getHp());
        builder.setPower(cricketTalent.getPower());
        return builder;
    }

    /**
     * 蛐蛐属性(战斗属性)
     */
    public static CricketProto.CricketPropertyTempMsg.Builder parseCricketPropertyTemp(CricketProperty cricketProperty) {
        if (cricketProperty == null) {
            return null;
        }
        CricketProto.CricketPropertyTempMsg.Builder builder = CricketProto.CricketPropertyTempMsg.newBuilder();
        builder.setAtk(cricketProperty.getAtk());
        builder.setHp(cricketProperty.getHp());
        builder.setPower(cricketProperty.getPower());
        builder.setAbility(cricketProperty.getAbility());
        return builder;
    }

    /**
     * 蛐蛐技能
     */
    public static CricketProto.CricketSkillTempMsg.Builder parseCricketSkillTemp(CricketSkill cricketSkill) {
        if (cricketSkill == null) {
            return null;
        }
        CricketProto.CricketSkillTempMsg.Builder builder = CricketProto.CricketSkillTempMsg.newBuilder();
        builder.setSkillId(cricketSkill.getSkillId());
        return builder;
    }

    public static CricketProto.CricketNormalSkillTempMsg.Builder parseCricketNormalSkillTemp(CricketEquipSkill cricketSkill) {
        if (cricketSkill == null) {
            return null;
        }
        CricketProto.CricketNormalSkillTempMsg.Builder builder = CricketProto.CricketNormalSkillTempMsg.newBuilder();
        builder.setSkillId(cricketSkill.getSkillId());
        builder.setLv(cricketSkill.getLv());
        return builder;
    }

    /**
     * 场景信息
     *
     * @param scenceData 场景信息
     */
    public static CricketProto.CricketScenceTempMsg.Builder parseCricketScenceTemp(CricketScenceData scenceData) {
        if (scenceData == null) {
            return null;
        }
        CricketProto.CricketScenceTempMsg.Builder builder = CricketProto.CricketScenceTempMsg.newBuilder();
        builder.setScenceId(scenceData.getScenceId());
        builder.setCatchTimes(scenceData.getCatchTimes());
        builder.setUpCricketId(scenceData.getUpCricketId());
        builder.setUpCount(scenceData.getUpCount());
        return builder;
    }

    public static List<CricketProto.CricketItemMsg> parseCricketItemMsgList(Property property) {
        List<CricketProto.CricketItemMsg> list = new ArrayList<>();
        for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
            CricketProto.CricketItemMsg.Builder builder = CricketProto.CricketItemMsg.newBuilder();
            builder.setItemId(entry.getKey());
            builder.setNum(entry.getValue().longValue());
            list.add(builder.build());
        }
        return list;
    }

    /**
     * 图鉴信息
     */
    public static CricketProto.CricketBookTempMsg.Builder parseCricketBookTempMsg(CricketBookData bookData) {
        if (bookData == null) {
            return null;
        }
        CricketProto.CricketBookTempMsg.Builder builder = CricketProto.CricketBookTempMsg.newBuilder();
        builder.setCricketId(bookData.getCricketId());
        builder.setTotalCount(bookData.getTotalCount());
        return builder;
    }

    /**
     * 获取蛐蛐匹配战斗数据
     *
     * @param cricketData
     * @return
     */
    public static CricketProto.CricketMatchDataTemp.Builder parseCricketMatchDataTemp(CricketData cricketData) {
        if (cricketData == null) {
            return null;
        }
        CricketProto.CricketMatchDataTemp.Builder builder = CricketProto.CricketMatchDataTemp.newBuilder();
        builder.setUserId(cricketData.getUserId());
        builder.setCricketId(cricketData.getCricketId());
        builder.setQuality(cricketData.getQuality());
        builder.setTitle(cricketData.getTitle());
        builder.setLittleTitle(cricketData.getLittleTitle());
        builder.setTalent(parseCricketTalentTemp(cricketData.getCricketTalent()));

        CricketProperty cricketProperty = cricketData.getCricketProperty().clone();
        CricketProperty addProperty = cricketData.getAddProperty();
        cricketProperty.setAtk(cricketProperty.getAtk() + addProperty.getAtk());
        cricketProperty.setHp(cricketProperty.getHp() + addProperty.getHp());
        cricketProperty.setPower(cricketProperty.getPower() + addProperty.getPower());
        cricketProperty.setAbility(cricketProperty.getAbility() + addProperty.getAbility());
        builder.setProperty(parseCricketPropertyTemp(cricketProperty));

        return builder;
    }

    public static CricketProto.CricketMatchDataTemp.Builder parseCricketMatchDataTemp(CricketProto.CricketTempMsg cricketTempMsg) {
        if (cricketTempMsg == null) {
            return null;
        }
        CricketProto.CricketMatchDataTemp.Builder builder = CricketProto.CricketMatchDataTemp.newBuilder();
        builder.setUserId(cricketTempMsg.getUserId());
        builder.setCricketId(cricketTempMsg.getCricketId());
        builder.setQuality(cricketTempMsg.getQuality());
        builder.setTitle(cricketTempMsg.getTitle());
        builder.setLittleTitle(cricketTempMsg.getLittleTitle());

        CricketProto.CricketPropertyTempMsg.Builder cricketProperty = CricketProto.CricketPropertyTempMsg.newBuilder();
        cricketProperty.setAtk(cricketTempMsg.getProperty().getAtk());
        cricketProperty.setAbility(cricketTempMsg.getProperty().getAbility());
        cricketProperty.setHp(cricketTempMsg.getProperty().getHp());
        cricketProperty.setPower(cricketTempMsg.getProperty().getPower());

        CricketProto.CricketPropertyTempMsg addProperty = cricketTempMsg.getAddProperty();
        cricketProperty.setAtk(cricketProperty.getAtk() + addProperty.getAtk());
        cricketProperty.setHp(cricketProperty.getHp() + addProperty.getHp());
        cricketProperty.setPower(cricketProperty.getPower() + addProperty.getPower());
        cricketProperty.setAbility(cricketProperty.getAbility() + addProperty.getAbility());
        builder.setProperty(cricketProperty);

        return builder;
    }

    /**
     * 获取蛐蛐匹配战斗数据
     */
    public static CricketProto.CricketMatchDataTemp.Builder parseCricketMatchDataTemp(CricketCourtDefenseData defenseData) {
        if (defenseData == null) {
            return null;
        }
        CricketProto.CricketMatchDataTemp.Builder builder = CricketProto.CricketMatchDataTemp.newBuilder();
        builder.setUserId(defenseData.getUserId());
        builder.setCricketId(defenseData.getCricketId());
        builder.setQuality(defenseData.getQuality());
        builder.setTitle(defenseData.getTitle());
        builder.setLittleTitle(defenseData.getLittleTitle());
        builder.setTalent(parseCricketTalentTemp(defenseData.getTalent()));
        builder.setProperty(parseCricketPropertyTemp(defenseData.getProperty()));
        for (CricketSkill cricketSkill : defenseData.getBattleSkill()) {
            builder.addBattleSkill(parseCricketSkillTemp(cricketSkill));
        }
        for (CricketSkill cricketSkill : defenseData.getAttackSkill()) {
            builder.addAttackSkill(parseCricketSkillTemp(cricketSkill));
        }
        for (CricketSkill cricketSkill : defenseData.getInitSkill()) {
            builder.addInitSkill(parseCricketSkillTemp(cricketSkill));
        }
        for (CricketSkill cricketSkill : defenseData.getSkill()) {
            builder.addSkill(parseCricketSkillTemp(cricketSkill));
        }
        return builder;
    }

    public static CricketTalent parseCricketTalent(CricketProto.CricketTalentTempMsg temp) {
        CricketTalent talent = new CricketTalent();
        talent.setAtk(temp.getAtk());
        talent.setHp(temp.getHp());
        talent.setPower(temp.getPower());
        return talent;
    }

    public static CricketProperty parseCricketProperty(CricketProto.CricketPropertyTempMsg temp) {
        CricketProperty property = new CricketProperty();
        property.setAtk(temp.getAtk());
        property.setHp(temp.getHp());
        property.setPower(temp.getPower());
        property.setAbility(temp.getAbility());
        return property;
    }

    public static CricketSkill parseCricketSkill(CricketProto.CricketSkillTempMsg temp) {
        CricketSkill skill = new CricketSkill();
        skill.setSkillId(temp.getSkillId());
        return skill;
    }

    public static CricketCourtDefenseData parseCricketCourtDefenseData(CricketProto.CricketMatchDataTemp temp) {
        CricketCourtDefenseData defenseData = new CricketCourtDefenseData();
        defenseData.setUserId(temp.getUserId());
        defenseData.setCricketId(temp.getCricketId());
        defenseData.setQuality(temp.getQuality());
        defenseData.setTitle(temp.getTitle());
        defenseData.setLittleTitle(temp.getLittleTitle());
        defenseData.setTalent(parseCricketTalent(temp.getTalent()));
        defenseData.setProperty(parseCricketProperty(temp.getProperty()));
        for (CricketProto.CricketSkillTempMsg cricketSkillTempMsg : temp.getBattleSkillList()) {
            defenseData.getBattleSkill().add(parseCricketSkill(cricketSkillTempMsg));
        }
        for (CricketProto.CricketSkillTempMsg cricketSkillTempMsg : temp.getAttackSkillList()) {
            defenseData.getAttackSkill().add(parseCricketSkill(cricketSkillTempMsg));
        }
        for (CricketProto.CricketSkillTempMsg cricketSkillTempMsg : temp.getInitSkillList()) {
            defenseData.getInitSkill().add(parseCricketSkill(cricketSkillTempMsg));
        }
        for (CricketProto.CricketSkillTempMsg cricketSkillTempMsg : temp.getSkillList()) {
            defenseData.getSkill().add(parseCricketSkill(cricketSkillTempMsg));
        }
        return defenseData;
    }


    public static CricketProto.CricketUserBattleMatchDataTemp.Builder parseCricketUserBattleMatchDataTemp(CricketCourtBattleData battleData) {
        CricketProto.CricketUserBattleMatchDataTemp.Builder builder = CricketProto.CricketUserBattleMatchDataTemp.newBuilder();
        CricketProto.CricketUserBattleDataTemp.Builder battleDataTemp = CricketProto.CricketUserBattleDataTemp.newBuilder();
        battleDataTemp.setUserId(battleData.getUserId());
        battleDataTemp.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(battleData.getUserBaseInfo()));
        battleDataTemp.setScore(battleData.getScore());
        battleDataTemp.setCourtBadgeId(battleData.getCourtBadgeId());
        builder.setUserInfo(battleDataTemp);
        for (CricketCourtDefenseData defenseData : battleData.getDefenseDataList()) {
            builder.addDefense(parseCricketMatchDataTemp(defenseData));
        }
        builder.setWinScore(battleData.getWinScore());
        builder.setFailScore(battleData.getFailScore());
        builder.setDefenderWinScore(battleData.getDefenderWinScore());
        builder.setDefenderFailScore(battleData.getDefenderFailScore());
        return builder;
    }

    public static CricketProto.CricketCourtConfigMsg.Builder parseCricketCourtConfigMsg(CricketCourtConfig courtConfig) {
        if (courtConfig == null) {
            return null;
        }
        CricketProto.CricketCourtConfigMsg.Builder builder = CricketProto.CricketCourtConfigMsg.newBuilder();
        builder.setCourtId(courtConfig.getCourtId());
        builder.setCourtType(courtConfig.getCourtType());
        builder.setCourtName(courtConfig.getCourtName());
        builder.setNum(courtConfig.getNum());
        builder.setUpNum(courtConfig.getUpNum());
        builder.setKeepNum(courtConfig.getKeepNum());
        builder.setReward(courtConfig.getReward());
        builder.setRankReward(courtConfig.getRankReward());
        builder.setCourtState(courtConfig.getCourtState());
        return builder;
    }

}